package ua.yandex.skipass;

import java.util.Calendar;

public class WeekdayPass extends SkiPass {
    private Calendar expirationDate;
    private final DateChecker dateChecker;

    WeekdayPass(WeekdayDayType weekdayDayType, Calendar releaseDate) {
        super(PassType.WEEKDAY);
        setExpirationDate(weekdayDayType, releaseDate);
        dateChecker = weekdayDayType.getDateChecker();
    }

    private void setExpirationDate(DayType dayType, Calendar releaseDate) {
        int releaseHour = releaseDate.get(Calendar.HOUR_OF_DAY);
        expirationDate = (Calendar) releaseDate.clone();
        if (releaseHour < dayType.getValidUntilHour()) {
            expirationDate.add(Calendar.DAY_OF_MONTH, dayType.duration());
        } else {
            expirationDate.add(Calendar.DAY_OF_MONTH, 1 + dayType.duration());
        }
    }

    @Override
    public Calendar getExpirationDate() {
        return expirationDate;
    }

    @Override
    public boolean validAt(Calendar someTime) {
        return dateChecker.validAt(someTime);
    }
}
